package com.classroompie.classroompieserver.service.impl;

import com.classroompie.classroompiecommon.context.BaseContext;
import com.classroompie.classroompiepojo.entity.TopicMessageModel;
import com.classroompie.classroompiepojo.entity.TopicModel;
import com.classroompie.classroompieserver.mapper.TopicMapper;
import com.classroompie.classroompieserver.mapper.TopicMessageMapper;
import com.classroompie.classroompieserver.service.TopicService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static com.classroompie.classroompiepojo.entity.table.TopicMessageModelTableDef.TOPIC_MESSAGE_MODEL;
import static com.classroompie.classroompiepojo.entity.table.TopicModelTableDef.TOPIC_MODEL;

/**
 * @author jiahao
 * @since 2024/6/28 周五
 */
@Service
public class TopicServiceImpl implements TopicService {
    private final TopicMapper topicMapper;

    private final TopicMessageMapper topicMessageMapper;

    public TopicServiceImpl(TopicMapper topicMapper, TopicMessageMapper topicMessageMapper) {
        this.topicMapper = topicMapper;
        this.topicMessageMapper = topicMessageMapper;
    }

    @Override
    public void addTopic(String courseId,String title) {
        String currentId = BaseContext.getCurrentId();
        TopicModel topicModel = new TopicModel();
        topicModel.setTitle(title);
        topicModel.setCourseId(courseId);
        topicModel.setCreateUser(currentId);
        topicModel.setCreateAt(LocalDateTime.now());
        topicMapper.insert(topicModel);
    }

    @Override
    public List<TopicModel> getTopic(String courseId) {
        return topicMapper.selectListByCondition(TOPIC_MODEL.COURSE_ID.eq(courseId));
    }

    @Override
    public void addMessage(String topicId, String message) {
        TopicMessageModel topicMessageModel = new TopicMessageModel();
        topicMessageModel.setTId(topicId);
        topicMessageModel.setMessage(message);
        topicMessageModel.setCreateAt(LocalDateTime.now());
        topicMessageModel.setCreateUser(BaseContext.getCurrentId());
        topicMessageMapper.insert(topicMessageModel);
    }

    @Override
    public List<TopicMessageModel> getMessage(String topicId) {
        return topicMessageMapper.selectListByCondition(TOPIC_MESSAGE_MODEL.T_ID.eq(topicId));
    }

    @Override
    public TopicModel getTopicDetail(String topicId) {
        return topicMapper.selectOneById(topicId);
    }
}
